﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMukti.Presentation.Controls;
using System.Runtime.InteropServices;
using VMukti.Business;
using VMukti.Business.CommonDataContracts;
using VMuktiAPI;
//using POD;
//using POD.Common;

namespace VMuktiGrid.CustomGrid
{
    public partial class ctlGrid : UserControl
    {
        ctlPOD objPOD = null;
        ItemsControl knownParent = null;
        ItemsControl targetItemsControl;
        List<int> lstDeletePods = new List<int>();

        public Rectangle rectSuggetion = new Rectangle();
        public bool IsDragEnter = true;

        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        public ctlGrid()
        {
            try
            {
                InitializeComponent();

                rectSuggetion.Fill = Brushes.Transparent;
                rectSuggetion.Stroke = Brushes.Red;

                DoubleCollection dblCol = new DoubleCollection();
                dblCol.Add(5.0);
                dblCol.Add(5.0);

                rectSuggetion.StrokeDashArray = dblCol;
                rectSuggetion.StrokeDashCap = PenLineCap.Round;
                rectSuggetion.StrokeDashOffset = 50;
                rectSuggetion.StrokeEndLineCap = PenLineCap.Square;
                rectSuggetion.StrokeLineJoin = PenLineJoin.Miter;
                rectSuggetion.StrokeMiterLimit = 50;
                rectSuggetion.RadiusX = 16;
                rectSuggetion.RadiusY = 16;
                rectSuggetion.Height = 100;

                EventManager.RegisterClassHandler(typeof(ctlPOD), ctlPOD.OnPODDragEvent, new RoutedEventHandler(this.OnDragPOD));
                EventManager.RegisterClassHandler(typeof(ctlPOD), ctlPOD.OnPODDropEvent, new RoutedEventHandler(this.OnDropPOD));

                LeftPanelContainer.MouseLeave += new MouseEventHandler(LeftPanelContainer_MouseLeave);
                CentralPanelContainer.MouseLeave += new MouseEventHandler(CentralPanelContainer_MouseLeave);
                RightPanelContainer.MouseLeave += new MouseEventHandler(RightPanelContainer_MouseLeave);

                LeftPanelContainer.MouseEnter += new MouseEventHandler(LeftPanelContainer_MouseEnter);
                CentralPanelContainer.MouseEnter += new MouseEventHandler(CentralPanelContainer_MouseEnter);
                RightPanelContainer.MouseEnter += new MouseEventHandler(RightPanelContainer_MouseEnter);


                LeftPanelContainer.AllowDrop = true;
                CentralPanelContainer.AllowDrop = true;
                RightPanelContainer.AllowDrop = true;

                LeftPanelContainer.Tag = 1;
                CentralPanelContainer.Tag = 2;
                RightPanelContainer.Tag = 3;

                //this.DragEnter += new DragEventHandler(ctlGrid_DragEnter);
                this.PreviewDragEnter += new DragEventHandler(ctlGrid_PreviewDragEnter);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlGrid()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void ctlGrid_PreviewDragEnter(object sender, DragEventArgs e)
        {
            try
            {
                this.DragEnter -= new DragEventHandler(ctlGrid_PreviewDragEnter);
                IsDragEnter = false;

                string[] strTag = ((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Tag.ToString().Split(',');
                //MessageBox.Show(((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Content.ToString());
                if (strTag.Length == 3)
                {
                    keybd_event(27, 0, 0, 0);
                    keybd_event(27, 0, 2, 0);

                    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(int.Parse(strTag[0]), VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);
                    int[] arrPermissionValue = new int[objCPC.Count];
                    for (int percount = 0; percount < objCPC.Count; percount++)
                    {
                        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                    }
                    ctlPOD objTempPOD = new ctlPOD(int.Parse(strTag[0]), ((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Caption, strTag[2], null, arrPermissionValue, false, "fromLeftPane", LeftPanelContainer);
                    objTempPOD.OwnerPodIndex = VMukti.App.podCounter++;
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlGrid_PreviewDragEnter()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void LeftPanelContainer_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (VMuktiHelper.IsDraggingPOD && VMuktiHelper.IsRectSuggestAdded == false)
                {
                    rectSuggetion.Height = VMuktiHelper.RectSuggestHeight;
                    if (rectSuggetion.Parent == null)
                    {
                        LeftPanelContainer.Items.Add(rectSuggetion);
                    }
                    else
                    {
                        ((ItemsControl)rectSuggetion.Parent).Items.Remove(rectSuggetion);
                        LeftPanelContainer.Items.Add(rectSuggetion);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LeftPanelContainer_MouseEnter()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void CentralPanelContainer_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (VMuktiHelper.IsDraggingPOD && VMuktiHelper.IsRectSuggestAdded == false)
                {
                    rectSuggetion.Height = VMuktiHelper.RectSuggestHeight;
                    if (rectSuggetion.Parent == null)
                    {
                        CentralPanelContainer.Items.Add(rectSuggetion);
                    }
                    else
                    {
                        ((ItemsControl)rectSuggetion.Parent).Items.Remove(rectSuggetion);
                        CentralPanelContainer.Items.Add(rectSuggetion);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CentralPanelContainer_MouseEnter()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void RightPanelContainer_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (VMuktiHelper.IsDraggingPOD && VMuktiHelper.IsRectSuggestAdded == false)
                {
                    rectSuggetion.Height = VMuktiHelper.RectSuggestHeight;
                    if (rectSuggetion.Parent == null)
                    {
                        RightPanelContainer.Items.Add(rectSuggetion);
                    }
                    else
                    {
                        ((ItemsControl)rectSuggetion.Parent).Items.Remove(rectSuggetion);
                        RightPanelContainer.Items.Add(rectSuggetion);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "RightPanelContainer_MouseEnter()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void LeftPanelContainer_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                foreach (object UIItem in LeftPanelContainer.Items)
                {
                    if (UIItem.GetType() == typeof(Rectangle))
                    {
                        LeftPanelContainer.Items.Remove(UIItem);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LeftPanelContainer_MouseLeave()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void CentralPanelContainer_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                foreach (object UIItem in CentralPanelContainer.Items)
                {
                    if (UIItem.GetType() == typeof(Rectangle))
                    {
                        CentralPanelContainer.Items.Remove(UIItem);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CentralPanelContainer_MouseLeave()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void RightPanelContainer_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                foreach (object UIItem in RightPanelContainer.Items)
                {
                    if (UIItem.GetType() == typeof(Rectangle))
                    {
                        RightPanelContainer.Items.Remove(UIItem);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "RightPanelContainer_MouseLeave()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void OnDragPOD(object o, RoutedEventArgs args)
        {
            try
            {
                objPOD = o as ctlPOD;
                knownParent = this.GetItemsControlContainingPanel(objPOD);
                if (knownParent != null)
                {
                    knownParent.Items.Remove(objPOD);
                    ((VMuktiGrid.ctlTab.TabItem)this.Parent).IsSaved = false;
                    this.DocumentRoot.Children.Add(objPOD);

                    objPOD.Width = objPOD.RenderSize.Width;
                    objPOD.IsHitTestVisible = false;
                    objPOD.HorizontalAlignment = HorizontalAlignment.Left;
                    objPOD.VerticalAlignment = VerticalAlignment.Top;
                    Grid.SetColumnSpan(objPOD, this.DocumentRoot.ColumnDefinitions.Count);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "OnDragPOD()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void OnDropPOD(object o, RoutedEventArgs args)
        {
            try
            {
                objPOD.Width = Double.NaN;
                objPOD.HorizontalAlignment = HorizontalAlignment.Stretch;
                objPOD.IsHitTestVisible = true;
                this.DocumentRoot.Children.Remove(objPOD);

                //Hit-test to find out the ItemsControl under the mouse-pointer.
                targetItemsControl = null;
                VisualTreeHelper.HitTest(this.DocumentRoot, new HitTestFilterCallback(this.FilterHitTestResultsCallback), new HitTestResultCallback(this.HitTestResultCallback), new PointHitTestParameters(Mouse.PrimaryDevice.GetPosition(this.DocumentRoot)));
                if (targetItemsControl != null)
                {
                    ((VMuktiGrid.ctlTab.TabItem)this.Parent).IsSaved = false;
                    bool? IsRectFound = null;
                ab:
                    foreach (object UIItem in targetItemsControl.Items)
                    {
                        if (UIItem.GetType() == typeof(Rectangle) && IsRectFound == null)
                        {
                            if (IsRectFound == null)
                            {
                                IsRectFound = false;
                            }
                            if (!IsRectFound.Value)
                            {
                                int i = targetItemsControl.Items.IndexOf(UIItem);
                                targetItemsControl.Items.Remove(UIItem);
                                targetItemsControl.Items.Insert(i, objPOD);
                                objPOD.ColNo = (int)targetItemsControl.Tag;
                                IsRectFound = true;
                                goto ab;
                            }
                            else
                            {
                                targetItemsControl.Items.Remove(UIItem);
                                goto ab;
                            }
                        }

                    }
                    if (IsRectFound != null && !IsRectFound.Value)
                    {
                        MessageBox.Show(targetItemsControl.Name);
                        targetItemsControl.Items.Add(objPOD);
                    }
                }
                else
                {
                    bool? IsRectFound = null;
                ab:
                    foreach (object UIItem in knownParent.Items)
                    {
                        if (UIItem.GetType() == typeof(Rectangle) && IsRectFound == null)
                        {
                            if (IsRectFound == null)
                            {
                                IsRectFound = false;
                            }
                            if (!IsRectFound.Value)
                            {
                                int i = knownParent.Items.IndexOf(UIItem);
                                knownParent.Items.Remove(UIItem);
                                knownParent.Items.Insert(i, objPOD);
                                IsRectFound = true;
                                goto ab;
                            }
                            else
                            {
                                knownParent.Items.Remove(UIItem);
                                goto ab;
                            }
                        }
                    }
                    if (IsRectFound != null && !IsRectFound.Value)
                    {
                        knownParent.Items.Add(objPOD);
                    }
                }
                if (rectSuggetion.Parent != null)
                {
                    ((ItemsControl)rectSuggetion.Parent).Items.Remove(rectSuggetion);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "OnDropPOD()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private ItemsControl GetItemsControlContainingPanel(ctlPOD panel)
        {
            try
            {
                if (this.IsPanelInItemsControl(panel, this.LeftPanelContainer))
                {
                    return this.LeftPanelContainer;
                }
                else if (this.IsPanelInItemsControl(panel, this.CentralPanelContainer))
                {
                    return this.CentralPanelContainer;
                }
                else if (this.IsPanelInItemsControl(panel, this.RightPanelContainer))
                {
                    return this.RightPanelContainer;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "GetItemsControlContainingPanel()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        private bool IsPanelInItemsControl(ctlPOD panel, ItemsControl itemsControl)
        {
            try
            {
                for (int i = 0; i < itemsControl.Items.Count; i++)
                {
                    if (itemsControl.Items[i].GetType() == typeof(ctlPOD) && itemsControl.Items[i] == panel)
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "IsPanelInItemsControl()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
            return false;
        }

        private HitTestFilterBehavior FilterHitTestResultsCallback(DependencyObject target)
        {
            if (target.GetType().IsAssignableFrom(typeof(ItemsControl)))
            {
                targetItemsControl = target as ItemsControl;
                return HitTestFilterBehavior.Stop;
            }
            else
            {
                return HitTestFilterBehavior.Continue;
            }
        }

        private HitTestResultBehavior HitTestResultCallback(HitTestResult result)
        {
            return HitTestResultBehavior.Stop;
        }

        public void AddControl(string strTitle, int moduleID, int colNumber)
        {
            try
            {
                #region test data
                if (colNumber == 1)
                {
                    //Button objButton = new Button();
                    //objButton.Content = (11111 * moduleID).ToString();
                    //ctlPOD objPod = new ctlPOD(strTitle, moduleID, (UIElement)objButton);
                    //LeftPanelContainer.Items.Add(objPod);
                }
                else if (colNumber == 2)
                {
                    //Button objButton = new Button();
                    //objButton.Content = (11111 * moduleID).ToString();
                    //ctlPOD objPod = new ctlPOD(strTitle, moduleID, (UIElement)objButton);
                    //CentralPanelContainer.Items.Add(objPod);
                }
                else if (colNumber == 3)
                {
                    //Button objButton = new Button();
                    //objButton.Content = (11111 * moduleID).ToString();
                    //ctlPOD objPod = new ctlPOD(strTitle, moduleID, (UIElement)objButton);
                    //RightPanelContainer.Items.Add(objPod);
                }
                //else if (colNumber == 4)
                //{
                //    Button btnTemp = new Button();
                //    btnTemp.Height = 100;
                //    btnTemp.Content = strTitle;
                //    LeftPanelContainer.Items.Add(btnTemp);
                //}
                #endregion
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddControl()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void AddControl(int intModID, string strModTitle, string strIsCollaborative, string strURI, int[] arrPermissionValue, bool flag, string strFromWhere)
        {
            try
            {
                if (LeftPanelContainer.Items.Count == 0 || (LeftPanelContainer.Items.Count < CentralPanelContainer.Items.Count && LeftPanelContainer.Items.Count < RightPanelContainer.Items.Count) || (LeftPanelContainer.Items.Count == CentralPanelContainer.Items.Count && LeftPanelContainer.Items.Count == RightPanelContainer.Items.Count))
                {
                    ctlPOD objPod = new ctlPOD(intModID, strModTitle, strIsCollaborative, strURI, arrPermissionValue, flag, strFromWhere, LeftPanelContainer);
                    objPod.OwnerPodIndex = VMukti.App.podCounter++;
                }
                else if (CentralPanelContainer.Items.Count == 0 || (CentralPanelContainer.Items.Count < LeftPanelContainer.Items.Count && CentralPanelContainer.Items.Count < RightPanelContainer.Items.Count))
                {
                    ctlPOD objPod = new ctlPOD(intModID, strModTitle, strIsCollaborative, strURI, arrPermissionValue, flag, strFromWhere, CentralPanelContainer);
                    objPod.OwnerPodIndex = VMukti.App.podCounter++;
                }
                else if (RightPanelContainer.Items.Count == 0 || (RightPanelContainer.Items.Count < LeftPanelContainer.Items.Count && RightPanelContainer.Items.Count < CentralPanelContainer.Items.Count))
                {
                    ctlPOD objPod = new ctlPOD(intModID, strModTitle, strIsCollaborative, strURI, arrPermissionValue, flag, strFromWhere, RightPanelContainer);
                    objPod.OwnerPodIndex = VMukti.App.podCounter++;
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddControl()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void DocumentRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                ((ctlGrid)DocumentRoot.Parent).Height = ((ctlGrid)DocumentRoot.Parent).Height + (e.NewSize.Height - e.PreviousSize.Height);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "DocumentRoot_SizeChanged()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void Save(int tabID)
        {
            try
            {
                foreach (VMuktiGrid.CustomGrid.ctlPOD item in LeftPanelContainer.Items)
                {
                    item.ColNo = 1;
                    item.Save(tabID);
                }
                foreach (VMuktiGrid.CustomGrid.ctlPOD item in CentralPanelContainer.Items)
                {
                    item.ColNo = 2;
                    item.Save(tabID);
                }
                foreach (VMuktiGrid.CustomGrid.ctlPOD item in RightPanelContainer.Items)
                {
                    item.ColNo = 3;
                    item.Save(tabID);
                }
                VMukti.Business.VMuktiGrid.ClsPod tempPOD = new VMukti.Business.VMuktiGrid.ClsPod();
                for (int i = 0; i < lstDeletePods.Count; i++)
                {
                    tempPOD.Remove_Pod(lstDeletePods[i]);
                }
                lstDeletePods.Clear();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Save()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void LoadGrid(int tabID)
        {
            try
            {
                VMukti.Business.VMuktiGrid.ClsTab objTab = VMukti.Business.VMuktiGrid.ClsTab.Get_Tab(tabID);
                DocumentRoot.ColumnDefinitions[0].Width = new GridLength(objTab.C1Width, GridUnitType.Star);
                DocumentRoot.ColumnDefinitions[1].Width = new GridLength(objTab.C2Width, GridUnitType.Star);
                DocumentRoot.ColumnDefinitions[2].Width = new GridLength(objTab.C3Width, GridUnitType.Star);

                VMukti.Business.VMuktiGrid.ClsPodCollection objPODs = VMukti.Business.VMuktiGrid.ClsPodCollection.GetAll(tabID);
                foreach (VMukti.Business.VMuktiGrid.ClsPod item in objPODs)
                {
                    //AddControl(item.ModuleId, item.PodTitle, item.ModuleId item.ColumnId);
                    VMukti.Business.VMuktiGrid.ClsModule objModule = VMukti.Business.VMuktiGrid.ClsModule.GetPodModule(item.ModuleId);

                    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(item.ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);
                    int[] arrPermissionValue = new int[objCPC.Count];
                    for (int percount = 0; percount < objCPC.Count; percount++)
                    {
                        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                    }

                    if (item.ColumnId == 1)
                    {
                        LeftPanelContainer.Items.Add(new ctlPOD(item.ModuleId, item.PodTitle, objModule.IsCollaborative, null, arrPermissionValue, false, "fromDatabase", null));
                    }
                    else if (item.ColumnId == 2)
                    {
                        CentralPanelContainer.Items.Add(new ctlPOD(item.ModuleId, item.PodTitle, objModule.IsCollaborative, null, arrPermissionValue, false, "fromDatabase", null));
                    }
                    else if (item.ColumnId == 3)
                    {
                        RightPanelContainer.Items.Add(new ctlPOD(item.ModuleId, item.PodTitle, objModule.IsCollaborative, null, arrPermissionValue, false, "fromDatabase", null));
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadGrid()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void LoadMeetingGrid(clsTabInfo objTabInfo)
        {
            try
            {
                DocumentRoot.ColumnDefinitions[0].Width = new GridLength(objTabInfo.dblC1Width, GridUnitType.Star);
                DocumentRoot.ColumnDefinitions[1].Width = new GridLength(objTabInfo.dblC2Width, GridUnitType.Star);
                DocumentRoot.ColumnDefinitions[2].Width = new GridLength(objTabInfo.dblC3Width, GridUnitType.Star);

                int i = 0;
                int j = 0;

                for (i = 0; i < objTabInfo.objaPods.Length; i++)
                {
                    VMukti.Business.VMuktiGrid.ClsModule objModule = VMukti.Business.VMuktiGrid.ClsModule.GetPodModule(objTabInfo.objaPods[i].intModuleId);

                    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(objTabInfo.objaPods[i].intModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);
                    int[] arrPermissionValue = new int[objCPC.Count];
                    for (int percount = 0; percount < objCPC.Count; percount++)
                    {
                        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                    }

                    if (objTabInfo.objaPods[i].intColumnNumber == 1)
                    {
                        for (j = 0; j < LeftPanelContainer.Items.Count; j++)
                        {
                            //Pratik is stucked.....
                            if (((ctlPOD)LeftPanelContainer.Items[j]).OwnerPodIndex == objTabInfo.objaPods[i].intOwnerPodIndex)
                            {
                                break;
                            }
                        }
                        if (j == LeftPanelContainer.Items.Count)
                        {
                            ctlPOD objPod = null;
                            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                            {
                                objPod = new ctlPOD(objTabInfo.objaPods[i].intModuleId, objTabInfo.objaPods[i].strPodTitle, objModule.IsCollaborative, objTabInfo.objaPods[i].strUri[0], arrPermissionValue, false, "fromOtherPeer", LeftPanelContainer);
                            }
                            else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                            {
                                objPod = new ctlPOD(objTabInfo.objaPods[i].intModuleId, objTabInfo.objaPods[i].strPodTitle, objModule.IsCollaborative, objTabInfo.objaPods[i].strUri[1], arrPermissionValue, false, "fromOtherPeer", LeftPanelContainer);
                            }
                            objPod.OwnerPodIndex = objTabInfo.objaPods[i].intOwnerPodIndex;
                        }
                    }
                    else if (objTabInfo.objaPods[i].intColumnNumber == 2)
                    {
                        for (j = 0; j < CentralPanelContainer.Items.Count; j++)
                        {
                            //Pratik is stucked.....
                            if (((ctlPOD)CentralPanelContainer.Items[j]).OwnerPodIndex == objTabInfo.objaPods[i].intOwnerPodIndex)
                            {
                                break;
                            }
                        }
                        if (j == CentralPanelContainer.Items.Count)
                        {
                            ctlPOD objPod = null;
                            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                            {
                                objPod = new ctlPOD(objTabInfo.objaPods[i].intModuleId, objTabInfo.objaPods[i].strPodTitle, objModule.IsCollaborative, objTabInfo.objaPods[i].strUri[0], arrPermissionValue, false, "fromOtherPeer", CentralPanelContainer);
                            }
                            else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                            {
                                objPod = new ctlPOD(objTabInfo.objaPods[i].intModuleId, objTabInfo.objaPods[i].strPodTitle, objModule.IsCollaborative, objTabInfo.objaPods[i].strUri[1], arrPermissionValue, false, "fromOtherPeer", CentralPanelContainer);
                            }
                            objPod.OwnerPodIndex = objTabInfo.objaPods[i].intOwnerPodIndex;
                        }
                    }
                    else if (objTabInfo.objaPods[i].intColumnNumber == 3)
                    {
                        for (j = 0; j < RightPanelContainer.Items.Count; j++)
                        {
                            //Pratik is stucked.....
                            if (((ctlPOD)RightPanelContainer.Items[j]).OwnerPodIndex == objTabInfo.objaPods[i].intOwnerPodIndex)
                            {
                                break;
                            }
                        }
                        if (j == RightPanelContainer.Items.Count)
                        {
                            ctlPOD objPod = null;
                            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                            {
                                objPod = new ctlPOD(objTabInfo.objaPods[i].intModuleId, objTabInfo.objaPods[i].strPodTitle, objModule.IsCollaborative, objTabInfo.objaPods[i].strUri[0], arrPermissionValue, false, "fromOtherPeer", RightPanelContainer);
                            }
                            else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                            {
                                objPod = new ctlPOD(objTabInfo.objaPods[i].intModuleId, objTabInfo.objaPods[i].strPodTitle, objModule.IsCollaborative, objTabInfo.objaPods[i].strUri[1], arrPermissionValue, false, "fromOtherPeer", RightPanelContainer);
                            }
                            objPod.OwnerPodIndex = objTabInfo.objaPods[i].intOwnerPodIndex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadMeetingGrid()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
            //foreach (VMukti.Business.VMuktiGrid.ClsPod item in objPODs)
            //{
            //    //AddControl(item.ModuleId, item.PodTitle, item.ModuleId item.ColumnId);
            //    VMukti.Business.VMuktiGrid.ClsModule objModule = VMukti.Business.VMuktiGrid.ClsModule.GetPodModule(item.ModuleId);

            //    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(item.ModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);
            //    int[] arrPermissionValue = new int[objCPC.Count];
            //    for (int percount = 0; percount < objCPC.Count; percount++)
            //    {
            //        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
            //    }

            //    if (item.ColumnId == 1)
            //    {
            //        LeftPanelContainer.Items.Add(new ctlPOD(item.ModuleId, item.PodTitle, objModule.IsCollaborative, null, arrPermissionValue, false, null));
            //    }
            //    else if (item.ColumnId == 2)
            //    {
            //        CentralPanelContainer.Items.Add(new ctlPOD(item.ModuleId, item.PodTitle, objModule.IsCollaborative, null, arrPermissionValue, false, null));
            //    }
            //    else if (item.ColumnId == 3)
            //    {
            //        RightPanelContainer.Items.Add(new ctlPOD(item.ModuleId, item.PodTitle, objModule.IsCollaborative, null, arrPermissionValue, false, null));
            //    }
            //}
        }

        public void LoadNewMeetingGrid(clsTabInfo objTabInfo)
        {
            try
            {
                DocumentRoot.ColumnDefinitions[0].Width = new GridLength(objTabInfo.dblC1Width, GridUnitType.Star);
                DocumentRoot.ColumnDefinitions[1].Width = new GridLength(objTabInfo.dblC2Width, GridUnitType.Star);
                DocumentRoot.ColumnDefinitions[2].Width = new GridLength(objTabInfo.dblC3Width, GridUnitType.Star);

                foreach (clsPodInfo item in objTabInfo.objaPods)
                {
                    //AddControl(item.ModuleId, item.PodTitle, item.ModuleId item.ColumnId);
                    VMukti.Business.VMuktiGrid.ClsModule objModule = VMukti.Business.VMuktiGrid.ClsModule.GetPodModule(item.intModuleId);

                    ClsPermissionCollection objCPC = ClsPermissionCollection.Get_PermissionRefModule(item.intModuleId, VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);
                    int[] arrPermissionValue = new int[objCPC.Count];
                    for (int percount = 0; percount < objCPC.Count; percount++)
                    {
                        arrPermissionValue[percount] = objCPC[percount].PermissionValue;
                    }
                    ctlPOD objPod = null;
                    if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                    {
                        objPod = new ctlPOD(item.intModuleId, item.strPodTitle, objModule.IsCollaborative, item.strUri[0], arrPermissionValue, false, "fromOtherPeer", null);
                    }
                    else if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                    {
                        objPod = new ctlPOD(item.intModuleId, item.strPodTitle, objModule.IsCollaborative, item.strUri[1], arrPermissionValue, false, "fromOtherPeer", null);
                    }

                    if (item.intColumnNumber == 1)
                    {
                        LeftPanelContainer.Items.Add(objPod);
                        objPod.ColNo = 1;
                    }
                    else if (item.intColumnNumber == 2)
                    {
                        CentralPanelContainer.Items.Add(objPod);
                        objPod.ColNo = 2;
                    }
                    else if (item.intColumnNumber == 3)
                    {
                        RightPanelContainer.Items.Add(objPod);
                        objPod.ColNo = 3;
                    }
                    objPod.OwnerPodIndex = item.intOwnerPodIndex;

                    foreach (string strBuddyName in item.straPodBuddies)
                    {
                        objPod.AddBuddy(strBuddyName);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadNewMeetingGrid()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void DeletePOD(int podID)
        {
            try
            {
                lstDeletePods.Add(podID);
                ((VMuktiGrid.ctlTab.TabItem)this.Parent).IsSaved = false;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "DeletePOD()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
            //foreach (VMuktiGrid.CustomGrid.ctlPOD item in LeftPanelContainer.Items)
            //{
            //    item.ColNo = 1;
            //    item.Save(tabID);
            //}
            //foreach (VMuktiGrid.CustomGrid.ctlPOD item in CentralPanelContainer.Items)
            //{
            //    item.ColNo = 2;
            //    item.Save(tabID);
            //}
            //foreach (VMuktiGrid.CustomGrid.ctlPOD item in RightPanelContainer.Items)
            //{
            //    item.ColNo = 3;
            //    item.Save(tabID);
            //}
        }

        public void AddBuddy(string strBuddyName)
        {
            try
            {
                foreach (VMuktiGrid.CustomGrid.ctlPOD item in LeftPanelContainer.Items)
                {
                    item.AddBuddy(strBuddyName);
                }
                foreach (VMuktiGrid.CustomGrid.ctlPOD item in CentralPanelContainer.Items)
                {
                    item.AddBuddy(strBuddyName);
                }
                foreach (VMuktiGrid.CustomGrid.ctlPOD item in RightPanelContainer.Items)
                {
                    item.AddBuddy(strBuddyName);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddBuddy()--:--ctlGrid.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }
    }
}